#include "mobilefilelistitem.h"

#include <QFont>
#include <QVariant>
#include <QEvent>

const int ITEM_ICON_WIDTH = 42;
const int ITEM_ICON_HEIGHT = 42;

MobileFileListItem::MobileFileListItem(QPushButton *parent) : QPushButton(parent)
{
    initWidget();
}

MobileFileListItem::~MobileFileListItem() {}

void MobileFileListItem::initWidget()
{
    this->installEventFilter(this);
    QHBoxLayout *mainHLayout = new QHBoxLayout();

    QVBoxLayout *labelVLayout = new QVBoxLayout();
    //项按钮
    m_itemIconLabel = new QLabel(this);
    m_itemIconLabel->setFixedSize(ITEM_ICON_WIDTH, ITEM_ICON_HEIGHT);
    //项名称
    m_itemNameLabel = new QLabel(this);
    QFont font;
    font.setBold(true);
    m_itemNameLabel->setFont(font);
    //每一项总数
    m_itemCountLabel = new QLabel(this);
    m_itemCountLabel->setStyleSheet("color:#8F9399;font-size:12px");

    labelVLayout->setContentsMargins(0, 0, 0, 0);
    labelVLayout->addStretch();
    labelVLayout->addWidget(m_itemNameLabel);
    labelVLayout->addSpacing(4);
    labelVLayout->addWidget(m_itemCountLabel);
    labelVLayout->addStretch();

    QLabel *vectorLab = new QLabel(this);
    vectorLab->setFixedSize(8, 8);
    vectorLab->setPixmap(QPixmap(":/svg/Vector1.svg").scaled(8, 8));

    mainHLayout->setContentsMargins(19, 0, 13, 0);
    mainHLayout->setSpacing(0);
    mainHLayout->addWidget(m_itemIconLabel);
    mainHLayout->addSpacing(11);
    mainHLayout->addLayout(labelVLayout);
    mainHLayout->addStretch();
    mainHLayout->addWidget(vectorLab);
    setLayout(mainHLayout);

    QPalette palette = this->palette();
    palette.setColor(QPalette::Button, QColor("#F6F6F6"));
    this->setPalette(palette);

    this->setProperty("useButtonPalette", true);
}

//设置项按钮
void MobileFileListItem::setItemIcon(QString path)
{
    m_itemIconLabel->setPixmap(QIcon(path).pixmap(42, 42));
}

//设置项名称
void MobileFileListItem::setItemName(QString name)
{
    m_itemNameLabel->setText(name);
}

//设置项总数
void MobileFileListItem::setItemCount(int count)
{
    QString countItem = QString("%1 ").arg(count) + QString(tr("items")); // items项
    m_itemCountLabel->setText(countItem);
}

//设置所有文件label样式
void MobileFileListItem::setAllFile(QString file)
{
    QString allFile = QString("%1").arg(file);
    m_itemCountLabel->setText(allFile);
}

bool MobileFileListItem::eventFilter(QObject *watch, QEvent *event)
{
    if (watch == this) {
         if (event->type() == QEvent::Enter) {
             QPalette palette = this->palette();
             palette.setColor(QPalette::Button, QColor("#E6F1FE"));
             this->setPalette(palette);
         } else if (event->type() == QEvent::Leave) {
             QPalette palette = this->palette();
             palette.setColor(QPalette::Button, QColor("#F6F6F6"));
             this->setPalette(palette);
         }
     }
     return QObject::eventFilter(watch, event);
}
